import {
  createElement,
  _textNode,
  _append
} from './dom.js'
import { _type } from '../utils/index.js'

export const _listItem = (items, render) => {
  let type = _type(items)
  let children

  switch (type) {
    case 'string':
      children = _type(render) === 'function' ? render(items) : _textNode(items)
      break
    case 'object':
      children = _type(render) === 'function' ? render(items) : JSON.stringify(items)
      break
    case 'array':
      return items.map(t => _listItem(t, render))
  }
  return children ? createElement("li", {
    class: 'item'
  }, {}, children) : ''
}

// 列表
export const _list = (items, render) => {
  let ul = createElement("ul")
  if (Array.isArray(items)) {
    _listItem(items, render).forEach(t => _append(ul, t))
  } else {
    _append(ul, _listItem(items, render))
  }
  return ul
}
